import java.util.List;

public class Monitor {
    private Message message;
    private boolean isLoginSuccess = false;
    private boolean isRegisterSuccess = false;
    private boolean rootPasswordIsRight = false;
    private boolean addQuestionComplete = false;
    private int[] addUserResult = null;
    private int[] deleteUserResult = null;
    private int[] deleteQuestionResult = null;
    private List<HaveAnswer> allQuestions = null;
    public final Object registerLock = new Object();
    public final Object loginLock = new Object();
    public final Object checkRootLock = new Object();
    public final Object addUserLock = new Object();
    public final Object deleteUserLock = new Object();
    public final Object addQuestionLock = new Object();
    public final Object getAllQuestionsLock = new Object();
    public final Object deleteQuestionLock = new Object();
    public UserType userType;
    public User loginedUser;

    public Monitor() {
    }

//    public Monitor(Message message) {
//        this.message = message;
//    }

    private void monitorInit() {
        message = null;
        isLoginSuccess = false;
        isRegisterSuccess = false;
        userType = null;
        loginedUser = null;
        addUserResult = null;
        deleteUserResult = null;
        deleteQuestionResult = null;
        allQuestions = null;

    }

    /**
     * 开始检查来自服务器的消息
     * 根据消息做出对应的动作
     */
    public void startProcess(Message messageGet) {
        monitorInit();
        message = messageGet;
        switch (message.messageType) {
            case "registerSuccess":
                synchronized (registerLock) {
                    isRegisterSuccess = true;
                    registerLock.notify();
                    break;
                }
            case "registerFail":
                synchronized (registerLock) {
                    isRegisterSuccess = false;
                    registerLock.notify();
                    break;
                }

            case "loginSuccess":
                synchronized (loginLock) {
                    isLoginSuccess = true;
                    userType = checkUserType(message.messageBody);
                    loginedUser = (User) message.messageBody;
                    loginLock.notify();
//                    System.out.println("登录成功！欢迎您使用在线考试系统！");
                    break;
                }
            case "loginFail":
                synchronized (loginLock) {
                    isLoginSuccess = false;
                    loginLock.notify();
//                    System.out.println("登录失败!");
                    break;
                }
            case "rootPasswordRight":
                synchronized (checkRootLock) {
                    rootPasswordIsRight = true;
                    checkRootLock.notify();
                    break;
                }

            case "rootPasswordWrong":
                synchronized (checkRootLock) {
                    rootPasswordIsRight = false;
                    checkRootLock.notify();
                    break;
                }
            case "addResult":
                synchronized (addUserLock) {
                    addUserResult = (int[]) ((ConveyableObject)message.messageBody).object;
                    addUserLock.notify();
                    break;
                }
            case "deleteResult":
                synchronized (deleteUserLock) {
                    deleteUserResult = (int[]) ((ConveyableObject)message.messageBody).object;
                    deleteUserLock.notify();
                    break;
                }
            case "addQuestionComplete":
                synchronized (addQuestionLock) {
                    addQuestionComplete = true;
                    addQuestionLock.notify();
                    break;
                }
            case "allQuestions":
                synchronized (getAllQuestionsLock) {
                    Object o = ((ConveyableObject)message.messageBody).object;
                    allQuestions = MyTool.castList(o, HaveAnswer.class);
                    getAllQuestionsLock.notify();
                    break;
                }
            case "deleteQuestionComplete":
                synchronized (deleteQuestionLock) {
                    deleteQuestionResult = (int[]) ((ConveyableObject)message.messageBody).object;
                    deleteQuestionLock.notify();
                    break;
                }

        }
    }

    public boolean isLoginSuccess() {
        return isLoginSuccess;
    }

    public void setLoginSuccess(boolean loginSuccess) {
        isLoginSuccess = loginSuccess;
    }

    public boolean isRegisterSuccess() {
        return isRegisterSuccess;
    }

    public void setRegisterSuccess(boolean registerSuccess) {
        isRegisterSuccess = registerSuccess;
    }

    public boolean isRootPasswordIsRight() {
        return rootPasswordIsRight;
    }

    public void setRootPasswordIsRight(boolean rootPasswordIsRight) {
        this.rootPasswordIsRight = rootPasswordIsRight;
    }

    public int[] getAddUserResult() {
        return addUserResult;
    }

    public int[] getDeleteUserResult() {
        return deleteUserResult;
    }

    public List<HaveAnswer> getAllQuestions() {
        return allQuestions;
    }

    public int[] getDeleteQuestionResult() {
        return deleteQuestionResult;
    }

    public boolean isAddQuestionComplete() {
        return addQuestionComplete;
    }

    public void setAddQuestionComplete(boolean addQuestionComplete) {
        this.addQuestionComplete = addQuestionComplete;
    }

    private UserType checkUserType(Conveyable userNeedBeCheck) {
        if (userNeedBeCheck instanceof Administrator) {
            return UserType.ADMINISTRATOR;
        }
        else return UserType.STUDENT;
    }
}
